home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1995 November / Macworld Nov ’95.toast / Developers / Selection ƒ 2.5 / selWindow < prev    next >
Encoding:
Text File  |  1994-11-06  |  3.4 KB  |  179 lines  |  [TEXT/MSET]

  1. (*
  2.  
  3. Class SelWindow is a window that handles objects that can be selected.
  4. The idea here is to provide a standard protocol for all (most?) selectable objects.
  5. Any such object must properly respond to a standard set of messages.  The advantages
  6. of following this framework protocol is it becomes rather easy to implement the
  7. standard Macintosh user interface.  Selecting and interacting with objects is
  8. automatic.
  9.  
  10.  
  11.  
  12. The protocol messages are as follows, most methods will be self explanatory.
  13.  
  14. new:    ( wptr -- )    perform any one-time initialization, called along with new: to window
  15. activate:        what to do when the window activates
  16. deactivate:        what to do when the window deactivates
  17. idle:            The currently selected object will receive idle messages when the window does.
  18. draw:
  19.  
  20. click:
  21. key: ( char -- )
  22. release:        will be called upon a close: to the window
  23. hit?:  ( -- b )  true if the mouse "hit" on the object, only call after a mouse down
  24. alwaysActive?:  ( -- b)
  25. focus?:  ( -- b)  return true if this object should receive keystrokes or clipboard
  26.     messages once it is selected.  Note that  only one object at a time may have the
  27.     focus.  For example, we would not want to send the same keboard input to more than
  28.     one textedit object at the same time (at least that's the way we do things here).
  29.  
  30. cut:
  31. copy:
  32. paste:
  33. clear:
  34.  
  35. *)
  36.  
  37. :CLASS selWindow  super{ primitiveWin }
  38.     var currentSel    \ the currently selected object; could be a nullSelect
  39.     ptrlist SelList    \ a list of objects that can be selected
  40.  
  41. :m add:  { ^obj -- }
  42.     alive: super 
  43.     IF
  44.         self new: ^obj
  45.         draw: ^obj
  46.         ^obj add: SelList
  47.     ELSE ." can't add: objects unless a selwindow is new" abort
  48.     THEN
  49.     ;m
  50.  
  51.  
  52. :m enable:  { \ next -- }
  53.     enable: super
  54.     get: currentSel activate: **
  55.  
  56.     BEGIN
  57.         each: SelList
  58.     WHILE
  59.         -> next
  60.         alwaysActive?: next
  61.         get: currentSel next <> and \ if currentSel is already active don't do it again
  62.         IF activate: next THEN
  63.     REPEAT
  64.     ;m
  65.  
  66. :m disable:
  67.     BEGIN
  68.         each: SelList
  69.     WHILE
  70.         deactivate: **
  71.     REPEAT
  72.     
  73.     disable: super ;m
  74.  
  75. :m idle:
  76.     get: currentSel idle: **
  77.     ;m
  78.  
  79.  
  80. :m draw:
  81.     BEGIN
  82.         each: SelList
  83.     WHILE
  84.         draw: **
  85.     REPEAT
  86.     ;m
  87.     
  88. private
  89.  
  90. :m doContent:  { \ next -- } \ Must look for a hit on an object in the list.
  91.     BEGIN
  92.         each: SelList
  93.     WHILE
  94.         -> next
  95.         hit?: next
  96.         IF
  97.             \ You hit something. Is it already the current selection?
  98.             next get: currentSel =
  99.             IF
  100.                 \ You hit the current selection, so do its click method.
  101.                 click: next  ( next = currentSel )
  102.             ELSE
  103.                 \ you hit a different object, so...
  104.                 focus?: next IF
  105.                     \ we must change the focus to the next object
  106.                     get: currentSel deactivate: **
  107.                     activate: next
  108.                     draw: next
  109.                     next put: currentSel
  110.                     ELSE
  111.                         \ don't change the focus, just do a click:
  112.                         click: next
  113.                     THEN
  114.             THEN
  115.         uneach: SelList  exit  \ must stop this method here
  116.         THEN
  117.     REPEAT
  118.     ;m
  119.  
  120. public
  121.  
  122. :m content:
  123.     active: self
  124.     IF
  125.         doContent: self
  126.     ELSE
  127.         select: self
  128.     THEN ;m
  129.  
  130. :m key: ( char -- )
  131.     get: currentSel key: ** ;m
  132.  
  133. :m cut:
  134.     get: currentSel cut: ** ;m
  135.  
  136. :m copy:
  137.     get: currentSel copy: ** ;m
  138.  
  139. :m paste:
  140.     get: currentSel paste: ** ;m
  141.  
  142. :m clear:
  143.     get: currentSel clear: ** ;m
  144.     
  145. :m new:    ( taddr tlen procID vis goaway -- )
  146.     new: super
  147.     new: SelList
  148.     tnullselect put: currentSel
  149. ;m
  150.  
  151. :m makeCurrent: ( addr -- )
  152.     put: currentSel ;m
  153.  
  154. :m getnew:    ( resid -- )
  155.     getnew: super
  156.     new: SelList
  157.     tnullselect put: currentSel
  158. ;m
  159.  
  160. :m close:
  161.     BEGIN
  162.         each: SelList
  163.     WHILE
  164.         release: **
  165.     REPEAT
  166.     release: selList
  167.     close: super
  168.     ;m
  169.         
  170. ;CLASS
  171.  
  172. endload
  173.  
  174.  
  175. *** EXAMPLE USE
  176.  
  177. selwindow w
  178. test: w
  179.